﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Data;

namespace PayRoller.Manage.RuleSet
{
    public class UserEdit : ParentPage, IRequiresSessionState
    {
        // Fields
        protected Button bnSave;
        protected DropDownList ddlRule;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        public int Sid;
        protected TextBox txtUsername;

        // Methods
        protected void bnSave_Click(object sender, EventArgs e)
        {
            TuserinfoBN obn = new TuserinfoBN(this.Page);
            TuserinfoDT detail = obn.Get(Convert.ToInt32(base.Request["Sid"]));
            detail.Username = this.txtUsername.Text;
            detail.RuleSid = Convert.ToInt32(this.ddlRule.SelectedValue);
            if (obn.Edit(detail))
            {
                obn.Close();
                obn.Dispose();
                base.ClientScript.RegisterStartupScript(base.GetType(), "", "<script>alert('Success!');window.location='UserList.aspx';</script>");
            }
        }

        public void GetRuleList()
        {
            TsysruleBN ebn = new TsysruleBN(this.Page);
            DataTable list = ebn.GetList();
            ListItem item = new ListItem();
            for (int i = 0; i < list.Rows.Count; i++)
            {
                string str = list.Rows[i]["Sid"].ToString();
                item = new ListItem(list.Rows[i]["Name"].ToString(), str);
                this.ddlRule.Items.Add(item);
            }
            ebn.Close();
            ebn.Dispose();
        }

        public void GetUserInfo(int Sid)
        {
            TuserinfoBN obn = new TuserinfoBN(this.Page);
            TuserinfoDT odt = obn.Get(Sid);
            this.txtUsername.Text = odt.Username;
            this.ddlRule.SelectedValue = odt.RuleSid.ToString();
            obn.Close();
            obn.Dispose();
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "0011";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.bnSave.Attributes.Add("onclick", "return Check1();");
            if (!this.Page.IsPostBack)
            {
                this.GetRuleList();
                if (base.Request["Sid"] != null)
                {
                    this.Sid = Convert.ToInt32(base.Request["Sid"]);
                    this.GetUserInfo(this.Sid);
                }
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
